package Java_Concurrency_in_Practice.zhalan;

import java.util.concurrent.CyclicBarrier;

public class CellularAutometa {

	private final Board mainBoard;
	private final CyclicBarrier barrier;
	private final Worker[] workers;
	
	public CellularAutometa(Board board) {
		this.mainBoard = board;
		int count = Runtime.getRuntime().availableProcessors();
		this.barrier = new CyclicBarrier(count, new Runnable() {
			@Override
			public void run() {
				mainBoard.commitNewValues();
			}
		});
		this.workers = new Worker[count];
	}
	
	private class Worker implements Runnable {
		private final Board board;
		public Worker(Board board) {
			this.board = board;
		}

		@Override
		public void run() {
			while (!board.hasConverged()) {
				for (int x = 0; x < board.getMaxX(); x++) {
					for (int y = 0; y < board.getMaxY(); y++) {
						board.setNewValue(x, y, computeValue(x, y));
					}
				}
				
				try {
					barrier.await();
				} catch (Exception e) {
					return;
				}
				
			}
		}
		
		private Object computeValue(int x, int y) {
			return null;
		}
		
	}
	
	
	public void start() {
		for (int i = 0; i < workers.length; i++) {
			new Thread(workers[i]).start();
		}
		mainBoard.waitForConvergence();
	}
}
